<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tk="http://www.qdocuments.net" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="tk msxsl">
  <xsl:import href="Button.xslt"/>
  <xsl:import href="EditItems.xslt"/>
  <xsl:import href="TableFrame.xslt"/>
  <xsl:variable name="tk:Toolkit" select="/Toolkit"/>
  <xsl:variable name="tk:XmlFile"/>
  <xsl:variable name="tk:AllResolvers" select="$tk:XmlFile/Toolkit/tk:Module/tk:Resolvers"/>
  <!--xsl:variable name="tk:Tables" select="$tk:XmlFile/Toolkit/tk:Table"/-->
  <xsl:variable name="tk:Resolvers" select="$tk:XmlFile/Toolkit/tk:Module/tk:Resolvers/tk:Resolver[(@Style != 'None') and (not(@Type) or @Type != 'View')]"/>
  <xsl:variable name="tk:AllXmlData">
    <xsl:for-each select="$tk:AllResolvers/tk:Resolver">
      <xsl:variable name="Table" select="document(tk:Xml)/Toolkit/tk:Table"/>
      <tk:Table RegName="{tk:RegName}">
        <xsl:copy-of select="$Table/@*"/>
        <xsl:copy-of select="$Table/*"/>
      </tk:Table>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="tk:AllXmlNodeSet" select="msxsl:node-set($tk:AllXmlData)/tk:Table"/>
  <xsl:template name="tk:Content">
    <xsl:param name="Method"/>
    <xsl:if test="$Method='Insert' or $Method='Update'">
      <xsl:call-template name="tk:PostScript">
        <xsl:with-param name="Method" select="$Method"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:call-template name="tk:Display">
      <xsl:with-param name="Method" select="$Method"/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template name="tk:PostScript">
    <xsl:param name="Method"/>
    <xsl:param name="Tables"/>
    <script language="JavaScript">
      function Post2(webFile, dataSet, save)
      {
        var tableNames = "<xsl:for-each select="$tk:Resolvers">
        <xsl:variable name="Pos" select="position()"/>
        <xsl:apply-templates select="$tk:AllXmlNodeSet[@RegName=current()/tk:RegName]" mode="tk:Table">
          <xsl:with-param name="Pos" select="$Pos"/>
        </xsl:apply-templates>
      </xsl:for-each>";
        var fieldNames = "<xsl:for-each select="$tk:Resolvers">
        <xsl:variable name="Pos" select="position()"/>
        <xsl:apply-templates select="$tk:AllXmlNodeSet[@RegName=current()/tk:RegName]" mode="tk:Field">
          <xsl:with-param name="Method" select="$Method"/>
          <xsl:with-param name="Pos" select="$Pos"/>
        </xsl:apply-templates>
      </xsl:for-each>";
        PostData(tableNames, fieldNames, webFile, dataSet, save);
      }
    </script>
  </xsl:template>
  <xsl:template match="tk:Table" mode="tk:Table">
    <xsl:param name="Pos"/>
    <xsl:if test="$Pos!=1">,</xsl:if>
    <xsl:value-of select="@TableName"/>
  </xsl:template>
  <xsl:template match="tk:Table" mode="tk:Field">
    <xsl:param name="Method"/>
    <xsl:param name="Pos"/>
    <xsl:if test="$Pos!=1">;</xsl:if>
    <xsl:choose>
      <xsl:when test="$Method='Insert'">
        <xsl:apply-templates select="tk:Field[tk:Insert/@HtmlCtrl!= 'Detail']" mode="tk:Field">
          <xsl:with-param name="Method" select="$Method"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:when test="$Method='Update'">
        <xsl:apply-templates select="tk:Field[tk:Update/@HtmlCtrl!= 'Detail']" mode="tk:Field">
          <xsl:with-param name="Method" select="$Method"/>
        </xsl:apply-templates>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="tk:Field" mode="tk:Field">
    <xsl:param name="Method"/>
    <xsl:variable name="HtmlCtrl">
      <xsl:call-template name="tk:GetHtmlCtrl">
        <xsl:with-param name="Field" select="."/>
        <xsl:with-param name="Method" select="$Method"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="position()!=1">,</xsl:if>
    <xsl:value-of select="tk:FieldName"/>
    <xsl:if test="$HtmlCtrl='EasySearch'">
      <xsl:text>,hd</xsl:text>
      <xsl:value-of select="tk:FieldName"/>
    </xsl:if>
  </xsl:template>
  <xsl:template name="tk:Display">
    <xsl:param name="Method"/>
    <xsl:for-each select="$tk:Resolvers">
      <xsl:variable name="Style" select="@Style"/>
      <xsl:variable name="Tables" select="$tk:AllXmlNodeSet[@RegName=current()/tk:RegName]"/>
      <xsl:apply-templates select="$Tables" mode="tk:Display">
        <xsl:with-param name="Method" select="$Method"/>
        <xsl:with-param name="Style" select="$Style"/>
      </xsl:apply-templates>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="tk:Table" mode="tk:Display">
    <xsl:param name="Method"/>
    <xsl:param name="Style"/>
    <xsl:variable name="Data" select="$tk:Toolkit/*[local-name()=current()/@TableName]"/>
    <xsl:variable name="FieldList">
      <tk:Table TableName="{@TableName}"/>
      <xsl:choose>
        <xsl:when test="$Method='Insert'">
          <xsl:for-each select="tk:Field[tk:Insert]">
            <xsl:sort select="tk:Insert/@Order" data-type="number"/>
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </xsl:when>
        <xsl:when test="$Method='Update'">
          <xsl:for-each select="tk:Field[tk:Update]">
            <xsl:sort select="tk:Update/@Order" data-type="number"/>
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </xsl:when>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="FieldNodes" select="msxsl:node-set($FieldList)"/>
    <xsl:if test="@TableDesc != '' and count($tk:AllXmlNodeSet) &gt; 1">
      <tr>
        <th width="100%">
          <div align="left">
            <xsl:value-of select="@TableDesc"/>
          </div>
        </th>
        <th align="right" class="cur-hand" onclick="showContentList('{@TableName}');">
          <img src="../images/ico_listup.gif" id="imgContentList{@TableName}" border="0" align="absmiddle"/>
        </th>
      </tr>
    </xsl:if>
    <tr>
      <td colspan="2" class="td-bg" id="divContentList{@TableName}">
        <table width="100%" border="0" cellspacing="1" cellpadding="2" id="{@TableName}">
          <xsl:choose>
            <xsl:when test="$Style='Normal'">
              <xsl:call-template name="tk:NormalDisplay">
                <xsl:with-param name="FieldList" select="$FieldNodes"/>
                <xsl:with-param name="Data" select="$Data"/>
                <xsl:with-param name="Method" select="$Method"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:when test="$Style='Table'">
              <xsl:call-template name="tk:TableDisplay">
                <xsl:with-param name="FieldList" select="$FieldNodes"/>
                <xsl:with-param name="Data" select="$Data"/>
                <xsl:with-param name="Method" select="$Method"/>
                <xsl:with-param name="Table" select="."/>
                <xsl:with-param name="Fix" select="false()"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:when test="$Style='FixTable'">
              <xsl:call-template name="tk:TableDisplay">
                <xsl:with-param name="FieldList" select="$FieldNodes"/>
                <xsl:with-param name="Data" select="$Data"/>
                <xsl:with-param name="Method" select="$Method"/>
                <xsl:with-param name="Table" select="."/>
                <xsl:with-param name="Fix" select="true()"/>
              </xsl:call-template>
            </xsl:when>
          </xsl:choose>
        </table>
      </td>
    </tr>
  </xsl:template>
  <xsl:template name="tk:NormalDisplay">
    <xsl:param name="FieldList"/>
    <xsl:param name="Data"/>
    <xsl:param name="Method"/>
    <xsl:for-each select="$Data">
      <xsl:apply-templates select="$FieldList/tk:Field[1]" mode="tk:DataList">
        <xsl:with-param name="DataRecord" select="."/>
        <xsl:with-param name="FieldList" select="$FieldList"/>
        <xsl:with-param name="Method" select="$Method"/>
        <xsl:with-param name="Pos" select="position()"/>
      </xsl:apply-templates>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="tk:Field" mode="tk:DataList">
    <xsl:param name="FieldList"/>
    <xsl:param name="DataRecord"/>
    <xsl:param name="Method"/>
    <xsl:param name="Pos"/>
    <xsl:variable name="HtmlCtrl">
      <xsl:call-template name="tk:GetHtmlCtrl">
        <xsl:with-param name="Field" select="."/>
        <xsl:with-param name="Method" select="$Method"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$HtmlCtrl=''">
        <xsl:apply-templates select="following-sibling::tk:Field[1]" mode="tk:DataList">
          <xsl:with-param name="FieldList" select="$FieldList"/>
          <xsl:with-param name="DataRecord" select="$DataRecord"/>
          <xsl:with-param name="Method" select="$Method"/>
          <xsl:with-param name="Pos" select="$Pos"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:when test="$HtmlCtrl='Hidden'">
        <xsl:apply-templates select="." mode="tk:Item">
          <xsl:with-param name="DataRecord" select="$DataRecord"/>
          <xsl:with-param name="Method" select="$Method"/>
          <xsl:with-param name="Pos" select="$Pos"/>
          <xsl:with-param name="HtmlCtrl" select="$HtmlCtrl"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="following-sibling::tk:Field[1]" mode="tk:DataList">
          <xsl:with-param name="FieldList" select="$FieldList"/>
          <xsl:with-param name="DataRecord" select="$DataRecord"/>
          <xsl:with-param name="Method" select="$Method"/>
          <xsl:with-param name="Pos" select="$Pos"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="NextItemName">
          <xsl:if test="tk:Display/@IsTwoColumn='true'">
            <xsl:call-template name="tk:NextItem">
              <xsl:with-param name="Field" select="following-sibling::tk:Field[1]"/>
              <xsl:with-param name="DataRecord" select="$DataRecord"/>
              <xsl:with-param name="Method" select="$Method"/>
              <xsl:with-param name="Pos" select="$Pos"/>
            </xsl:call-template>
          </xsl:if>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="$NextItemName!=''">
            <xsl:variable name="NextFieldItem" select="following-sibling::tk:Field[tk:FieldName=$NextItemName]"/>
            <xsl:apply-templates select="." mode="tk:TableRow">
              <xsl:with-param name="FieldItem" select="."/>
              <xsl:with-param name="NextFieldItem" select="$NextFieldItem"/>
              <xsl:with-param name="DataRecord" select="$DataRecord"/>
              <xsl:with-param name="Pos" select="$Pos"/>
              <xsl:with-param name="Method" select="$Method"/>
              <xsl:with-param name="HtmlCtrl" select="$HtmlCtrl"/>
            </xsl:apply-templates>
            <xsl:for-each select="$NextFieldItem">
              <xsl:apply-templates select="following-sibling::tk:Field[1]" mode="tk:DataList">
                <xsl:with-param name="FieldList" select="$FieldList"/>
                <xsl:with-param name="DataRecord" select="$DataRecord"/>
                <xsl:with-param name="Method" select="$Method"/>
                <xsl:with-param name="Pos" select="$Pos"/>
              </xsl:apply-templates>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="." mode="tk:TableRow">
              <xsl:with-param name="FieldItem" select="."/>
              <xsl:with-param name="DataRecord" select="$DataRecord"/>
              <xsl:with-param name="Pos" select="$Pos"/>
              <xsl:with-param name="Method" select="$Method"/>
              <xsl:with-param name="HtmlCtrl" select="$HtmlCtrl"/>
            </xsl:apply-templates>
            <xsl:apply-templates select="following-sibling::tk:Field[1]" mode="tk:DataList">
              <xsl:with-param name="FieldList" select="$FieldList"/>
              <xsl:with-param name="DataRecord" select="$DataRecord"/>
              <xsl:with-param name="Method" select="$Method"/>
              <xsl:with-param name="Pos" select="$Pos"/>
            </xsl:apply-templates>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="tk:NextItem">
    <xsl:param name="Field"/>
    <xsl:param name="DataRecord"/>
    <xsl:param name="Method"/>
    <xsl:param name="Pos"/>
    <xsl:for-each select="$Field">
      <xsl:variable name="HtmlCtrl">
        <xsl:call-template name="tk:GetHtmlCtrl">
          <xsl:with-param name="Field" select="."/>
          <xsl:with-param name="Method" select="$Method"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:choose>
        <xsl:when test="$HtmlCtrl = ''">
          <xsl:call-template name="tk:NextItem">
            <xsl:with-param name="Field" select="following-sibling::tk:Field[1]"/>
            <xsl:with-param name="DataRecord" select="$DataRecord"/>
            <xsl:with-param name="Method" select="$Method"/>
            <xsl:with-param name="Pos" select="$Pos"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="$HtmlCtrl ='Hidden'">
          <xsl:apply-templates select="." mode="tk:Item">
            <xsl:with-param name="DataRecord" select="$DataRecord"/>
            <xsl:with-param name="Method" select="$Method"/>
            <xsl:with-param name="Pos" select="$Pos"/>
            <xsl:with-param name="HtmlCtrl" select="$HtmlCtrl"/>
          </xsl:apply-templates>
          <xsl:call-template name="tk:NextItem">
            <xsl:with-param name="Field" select="following-sibling::tk:Field[1]"/>
            <xsl:with-param name="DataRecord" select="$DataRecord"/>
            <xsl:with-param name="Method" select="$Method"/>
            <xsl:with-param name="Pos" select="$Pos"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:if test="tk:Display/@IsTwoColumn='true'">
            <xsl:value-of select="tk:FieldName"/>
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>
  <xsl:template name="tk:GetHtmlCtrl">
    <xsl:param name="Field"/>
    <xsl:param name="Method"/>
    <xsl:choose>
      <xsl:when test="$Method='Insert'">
        <xsl:value-of select="$Field/tk:Insert/@HtmlCtrl"/>
      </xsl:when>
      <xsl:when test="$Method='Update'">
        <xsl:value-of select="$Field/tk:Update/@HtmlCtrl"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="tk:Field" mode="tk:TableRow" name="tk:TableRow">
    <xsl:param name="FieldItem"/>
    <xsl:param name="NextFieldItem"/>
    <xsl:param name="DataRecord"/>
    <xsl:param name="Method"/>
    <xsl:param name="Pos"/>
    <xsl:param name="HtmlCtrl"/>
    <tr>
      <xsl:apply-templates select="." mode="tk:Item">
        <xsl:with-param name="DataRecord" select="$DataRecord"/>
        <xsl:with-param name="Method" select="$Method"/>
        <xsl:with-param name="Pos" select="$Pos"/>
        <xsl:with-param name="HtmlCtrl" select="$HtmlCtrl"/>
      </xsl:apply-templates>
      <xsl:if test="$NextFieldItem">
        <xsl:for-each select="$NextFieldItem">
          <xsl:variable name="NextHtmlCtrl">
            <xsl:call-template name="tk:GetHtmlCtrl">
              <xsl:with-param name="Field" select="."/>
              <xsl:with-param name="Method" select="$Method"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:apply-templates select="." mode="tk:Item">
            <xsl:with-param name="DataRecord" select="$DataRecord"/>
            <xsl:with-param name="Method" select="$Method"/>
            <xsl:with-param name="Pos" select="$Pos"/>
            <xsl:with-param name="HtmlCtrl" select="$NextHtmlCtrl"/>
          </xsl:apply-templates>
        </xsl:for-each>
      </xsl:if>
    </tr>
  </xsl:template>
  <xsl:template match="tk:Field" mode="tk:Item">
    <xsl:param name="DataRecord"/>
    <xsl:param name="Method"/>
    <xsl:param name="Pos"/>
    <xsl:param name="HtmlCtrl"/>
    <xsl:variable name="Value" select="$DataRecord/*[local-name()=current()/tk:FieldName]"/>
    <xsl:choose>
      <xsl:when test="$HtmlCtrl='Hidden'">
        <xsl:call-template name="tk:Hidden">
          <xsl:with-param name="Field" select="."/>
          <xsl:with-param name="Value" select="$Value"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$HtmlCtrl='Detail'">
        <xsl:call-template name="tk:Detail">
          <xsl:with-param name="Field" select="."/>
          <xsl:with-param name="Value" select="$Value"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$HtmlCtrl='Text' or $HtmlCtrl = 'CheckBox' or $HtmlCtrl='File' or $HtmlCtrl='Password'">
        <xsl:call-template name="tk:Input">
          <xsl:with-param name="Field" select="."/>
          <xsl:with-param name="Value" select="$Value"/>
          <xsl:with-param name="Pos" select="$Pos"/>
          <xsl:with-param name="HtmlCtrl" select="$HtmlCtrl"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$HtmlCtrl='Combo'">
        <xsl:call-template name="tk:Combo">
          <xsl:with-param name="Field" select="."/>
          <xsl:with-param name="Value" select="$Value"/>
          <xsl:with-param name="Pos" select="$Pos"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$HtmlCtrl='TextArea'">
        <xsl:call-template name="tk:TextArea">
          <xsl:with-param name="Field" select="."/>
          <xsl:with-param name="Value" select="$Value"/>
          <xsl:with-param name="Pos" select="$Pos"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$HtmlCtrl='Radio'">
        <xsl:call-template name="tk:Radio">
          <xsl:with-param name="Field" select="."/>
          <xsl:with-param name="Value" select="$Value"/>
          <xsl:with-param name="Pos" select="$Pos"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$HtmlCtrl='EasySearch'">
        <xsl:call-template name="tk:EasySearch">
          <xsl:with-param name="Field" select="."/>
          <xsl:with-param name="Value" select="$Value"/>
          <xsl:with-param name="Pos" select="$Pos"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$HtmlCtrl='Upload'">
        <xsl:call-template name="tk:Upload">
          <xsl:with-param name="Field" select="."/>
          <xsl:with-param name="Value" select="$Value"/>
          <xsl:with-param name="DataRecord" select="$DataRecord"/>
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
